import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Antd, { message } from 'ant-design-vue'
import 'ant-design-vue/dist/reset.css'
import * as Icons from '@ant-design/icons-vue'
import axios from 'axios'
import { Tool } from './utils/tool'

const app = createApp(App)
app.use(store).use(router).use(Antd).mount('#app')

console.log('环境', process.env.NODE_ENV);
console.log('服务器', process.env.VUE_APP_BACKEND_SERVER);
//配置请求前缀
axios.defaults.baseURL = process.env.VUE_APP_BACKEND_SERVER;

//axios拦截器
axios.interceptors.request.use(function (config: any) {
    console.log('请求参数', config);
    const token = store.state.user.token;
    if (Tool.isNotEmpty(token)) {
        config.headers.token = token;
        console.log("请求头中的token:", token);
    }
    return config;
}, error => {
    return Promise.reject(error);
}
);

//请求错误处理器
// function errorHandle(error: any) {
//     if (error.response) {
//         // The request was made and the server responded with a status code 
//         // 请求已发出，服务器用状态代码响应
//         // that falls out of the range of 2xx 超出了2xx的范围
//         const { status } = error.response;
//         if (status) {
//             if (error.message == 'Request failed with status code 401') {
//                 const errorText = "请先登录！";
//                 message.error(errorText);
//                 // alert(errorText);
//             }
//         }
//     } else {
//         message.error("请求失败！");
//     }
// }


axios.interceptors.response.use(function (response) {
    console.log('返回结果', response);
    return response;
}, error => {
    // errorHandle(error);
    console.log('返回错误', error);
    return Promise.reject(error);
}
);

//全局使用图标
const icons: any = Icons;
for (const i in icons) {
    app.component(i, icons[i]);
}
